﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AbstraX.Bindings
{
    public static class BindingExtensions
    {
        public static BindingsTree Tree(this IBindingsTreeNode node)
        {
            var tree = new BindingsTree();
            var nodeParent = node;
            IBindingsTreeNode lastParent = null;

            while (nodeParent != null)
            {
                lastParent = nodeParent;
                nodeParent = nodeParent.ParentNode;
            }

            AddToTree(lastParent, tree);

            return tree;
        }

        private static void AddToTree(IBindingsTreeNode node, BindingsTree tree)
        {
            tree.Add(node);

            foreach (var childNode in node.ChildNodes)
            {
                if (!childNode.IsReference)
                {
                    AddToTree(childNode, tree);
                }
            }
        }
    }
}
